<template>
  <el-container>
    <el-main>
      <el-tabs tab-position="left" style="height:620px;">
        <!-- 常规tab -->
        <el-tab-pane label="常规" class="main-h-center">
          <el-form label-width="150px" label-position="left" class="form">
            <el-form-item label="关闭面板操作：">
              <el-radio v-model="closeApp" label="0">最小化到托盘</el-radio>
              <el-radio v-model="closeApp" label="1">退出程序</el-radio>
            </el-form-item>

            <el-form-item label="清除页面缓存：">
              <el-button size="mini" round style="width:100px;" @click="clearPageCache">清除</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <!-- 下载tab -->
        <el-tab-pane label="下载" class="main-h-center">
          <el-form label-width="150px" label-position="left" class="form">
            <el-form-item label="下载路径：">
              <el-input v-model="downloadPath" style="width:300px" size="mini"></el-input>
              <el-button size="mini" round style="margin-left:10px;" @click="selectDownloadPath">浏览</el-button>
            </el-form-item>

            <el-form-item label="安装路径：">
              <el-input v-model="installPath" style="width:300px" size="mini"></el-input>
              <el-button size="mini" round style="margin-left:10px;" @click="selectInstallPath">浏览</el-button>
            </el-form-item>

            <el-form-item label="游戏安装后：">
              <el-radio v-model="installAfter" label="0">立即删除</el-radio>
              <el-radio v-model="installAfter" label="1">保留7天</el-radio>
              <el-radio v-model="installAfter" label="2">永久保留</el-radio>
            </el-form-item>

            <el-form-item label="清除下载缓存：">
              <el-button size="mini" round style="width:100px;" @click="clearDownloadCache">清除</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-main>
    <el-footer class="set-footer">
      <el-row>
        <el-col :span="24" style>
          <el-button size="medium" round style="width:100px;" @click="saveConfig">确定</el-button>
        </el-col>
      </el-row>
    </el-footer>
  </el-container>
</template>
<script>
import set from "@/modules/set";
export default {
  data() {
    return {
      closeApp: "0",
      installAfter: "2",
      downloadPath: "",
      installPath: ""
    };
  },
  methods: {
    // 保存配置
    async saveConfig() {
      set.saveConfig({
        closeApp: this.closeApp,
        installAfter: this.installAfter,
        downloadPath: this.downloadPath,
        installPath: this.installPath
      });
      this.$message({
        offset: 100,
        showClose: true,
        message: "保存成功！",
        type: "success"
      });
    },
    // 选择安装路径
    selectInstallPath() {
      set.selectDir(path => {
        this.installPath = path;
      });
    },

    // 选择下载路径
    selectDownloadPath() {
      set.selectDir(path => {
        this.downloadPath = path;
      });
    },

    // 清理页面数据
    clearPageCache() {
      this.$confirm(
        "此操作会删除您保存的配置数据与页面缓存数据，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        set.clearConfig();
        let config = set.getConfig();
        this.closeApp = config.closeApp;
        this.installAfter = config.installAfter;
        this.downloadPath = config.downloadPath;
        this.installPath = config.installPath;
      });
    },

    // 清理下载路径
    async clearDownloadCache() {
      await this.$confirm("此操作会删除您下载的数据，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      });
      let err = await set.clearDir(this.downloadPath);
      if (!err) {
        this.$message({
          showClose: true,
          offset: 100,
          message: "清理成功",
          type: "success"
        });
      }
    }
  },
  async created() {
    let config = set.getConfig();
    this.closeApp = config.closeApp;
    this.installAfter = config.installAfter;
    this.downloadPath = config.downloadPath;
    this.installPath = config.installPath;
  }
};
</script>
<style scoped>
.set-footer {
  background-color: #f9f9f9;
  border-top: 1px solid #e4e4e4;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.form {
  width: 540px;
  padding-left: 20px;
}
</style>